package com.example.smarthome.ui.masterbedroom;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smarthome.R;
import com.example.smarthome.ui.home.AirCollect;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MasterbedroomFengshanActivity extends AppCompatActivity {

    String model="";
    String wendu="";
    int flag=0;
    private static final String DeviceID2="1064814621";
    private static final String ApiKey ="1SM9vECD95xFxkRzxivzktUNQko=";
    TextView data1,data2,data3;
    String value1,value2,value3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_masterbedroom_fengshan);
        Button button1 = (Button)findViewById(R.id.confirm);
        Button button2= (Button) findViewById(R.id.add);
        Button button3= (Button) findViewById(R.id.reduce);
        RadioButton radioButton =(RadioButton) findViewById(R.id.cold) ;
        RadioButton radioButton1=(RadioButton) findViewById(R.id.hot) ;
        EditText editText=(EditText) findViewById(R.id.Edix_text_wendu);
        wendu= editText.getText().toString();
        model=radioButton.getText().toString();
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                flag=1;
                Log.e("输入的温度当前", wendu);
                Log.e("输入的温度当前", model);
                if(model=="制冷")
                    lightControl("D");
                if(model=="制热")
                    lightControl("C");
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text= editText.getText().toString();
                int i=Integer.valueOf(text).intValue()+1;
                wendu = Integer.toString(i);
                editText.setText(wendu);
                Log.e("输入的温度当前", wendu);
                if(flag==0){
                    Toast.makeText(MasterbedroomFengshanActivity.this, "空调还未打开", Toast.LENGTH_SHORT).show();
                }
                else{
                    lightControl("C");
                }
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text= editText.getText().toString();
                int i=Integer.valueOf(text).intValue()-1;
                wendu = Integer.toString(i);
                editText.setText(wendu);
                Log.e("输入的温度当前", wendu);
                if(flag==0){
                    Toast.makeText(MasterbedroomFengshanActivity.this, "空调还未打开", Toast.LENGTH_SHORT).show();
                }
                else{
                    lightControl("D");
                }
            }
        });

       radioButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String text=radioButton.getText().toString();
               model=text;
               Log.e("输入的模式当前", text);
           }
       });
       radioButton1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String text=radioButton1.getText().toString();
               model=text;
               Log.e("输入的模式当前", text);
           }
       });
    }

    public void lightControl( String index) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String cmd=index;
                String body=String.format(cmd);
                String url=String.format("http://api.heclouds.com/cmds?device_id="+DeviceID2);
                OkHttpClient client=new OkHttpClient();
                RequestBody requestBody =RequestBody.create(MediaType.parse("application/json;charset=utf-8"),body);
                Request request=new Request.Builder().url(url).header("api-key",ApiKey).post(requestBody).build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        String insertStr="请求失败";
                        Log.i("请求失败","22");
//                        Toast.makeText(MasterbedroomFragment.this,insertStr, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                    }
                });
            }
        }).start();
    }
}